<?php
require_once realpath(dirname(__FILE__) . '//..//pages.php');
require_once CLASS_USER;
require_once INC_DEFINES;
require_once INC_FUNCTIONS;
require_once INC_MESSAGES;
require_once MDL_ACCOUNT;

checkSession ();

if (isset ($_SESSION['data'])) {
    $post = true;
    $data = array_merge ($_GET, $_SESSION['data']);
} else {
    $post = false;
    $data = $_GET;
}

if (isset ($data['account'])) {
    $cmd = $data['account'];
    switch ($cmd) {
        case 'login' :
            if ($post) {
                if (isset ($data['username']) && isset ($data['password'])) {
                    $user = $data['username'];
                    $pass = $data['password'];
                    if (login ($user, $pass)) {
                        if (isset ($data['redirect'])) {
                            $url = $data['redirect'];
                        } else {
                            $url = HTTP_CTRL_MAIN . '?request=view';
                        }
                        ?><meta http-equiv="refresh" content="0;url='<?php echo $url; ?>'" /><?php
                    } else {
                        $msg = MSG_FAILED_LOGIN;
                        require_once VW_NOTIFY;
                    }
                } else {
                    if (isset ($_SESSION[KEY_SESSION_USER])) {
                        $msg = 'You are already logged!';
                        require_once VW_NOTIFY;
                    } else {
                        $msg = 'Username and/or password not set!';
                        require_once VW_NOTIFY;
                    }
                }
            } else {
                require_once VW_LOGIN;
            }
            break;
        case 'logout' :
            logout ();
            ?><meta http-equiv="refresh" content="0;url='<?php echo HTTP_CTRL_MAIN; ?>?account=login'" /><?php
            break;
    }
}
?>